package com.democxy.modules.cms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

import com.democxy.common.baseData.ResponeData;
import com.democxy.common.enums.ResultEnum;
import com.democxy.common.utils.StringUtils;
import com.democxy.modules.cms.entity.BlogType;
import com.democxy.modules.cms.service.BlogTypeService;


/**
 * 博客类型Controller
 * @author democxy
 * @version v1.0
 */
@Controller
@RequestMapping(value = "/admin/cms/blogType")
public class BlogTypeController{

	@Autowired
	private BlogTypeService blogTypeService;
	
	@ResponseBody
	@RequestMapping(value = "list")
	public ResponeData<PageInfo<BlogType>> list(@RequestBody BlogType blogType) {
		PageHelper.startPage(blogType.getPageNo(),10);
		List<BlogType> list = blogTypeService.findList(blogType);
		PageInfo<BlogType> pageInfo = new PageInfo<BlogType>(list, 10);
		return new ResponeData<PageInfo<BlogType>>(pageInfo);
	}

	@ResponseBody
	@RequestMapping(value = "save")
	public ResponeData<String> save(@RequestBody BlogType blogType) {
		blogTypeService.save(blogType);
		return new ResponeData<String>("添加成功！");
	}
	

	@ResponseBody
	@RequestMapping(value = "delete")
	public ResponeData<String> delete(@RequestBody BlogType blogType) {
		if ("1".equals(blogType.getId())) {
			return new ResponeData<String>(ResultEnum.FAILED, "超级类目不允许删除！");
		}else {
			BlogType byId = blogTypeService.getById(blogType.getId());
			int countTotal = 0;
			if ("1".equals(byId.getType())){
				countTotal = blogTypeService.countTotal(blogType.getId());
			}else{
				countTotal = blogTypeService.countTotalLabels(blogType.getId());
			}
			if (countTotal>0) {
				return new ResponeData<String>(ResultEnum.FAILED, "该类目下有内容不允许删除！");
			}else {
				blogTypeService.delete(blogType.getId());
			}
		}
		return new ResponeData<String>("删除成功！");
	}

}